var _currentuser;
function plusReady() {
    _currentuser = JSON.parse(plus.storage.getItem(storageManager.user));

    // 广告
    PaintAdv();
    //初始化
    init();
}

if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}

function init() {
    if (2 == _currentuser.user_type || 3 == _currentuser.user_type) {
        paintStoreUserWallet(_currentuser.user_type);
    } else {
        paintAccontUserWallet();
    }

    var account = _currentuser.account;
    if (account) {
        $("#cash").html(account.cash);
        $("#card").html(account.card);
        $("#coin").html(account.coin);
        $("#frozen").html(account.frozen);
    }
    
    //余额转出
	$("#transfer a").on("click", function(){
		if( 0 == _currentuser.auth_status ){
			plus.ui.alert("对不起您还未通过实名认证", function() { 
				clicked("../../account/unautherized.html", false, false, "pop-in"); 
			}, configManager.alerttip, configManager.alertCtip);
		}
		else{
			clicked('transfer/choose.html', false, false, 'pop-in');
		}
	});
	//绑定银行卡
	$("#bbnone a").on("click", function(){
		if( 0 == _currentuser.auth_status ){
			plus.ui.alert("对不起您还未通过实名认证", function() { 
				clicked("../../account/unautherized.html", false, false, "pop-in"); 
			}, configManager.alerttip, configManager.alertCtip);
		}
		else{
			clicked('bankcard/manager.html', false, false, 'pop-in');
		}
	});
	//充值
	$("#charge a").on("click", function(){
		if( 0 == _currentuser.auth_status ){
			plus.ui.alert("对不起您还未通过实名认证", function() { 
				clicked("../../account/unautherized.html", false, false, "pop-in"); 
			}, configManager.alerttip, configManager.alertCtip);
		}
		else{
			clicked('charge/choose.html', false, false, 'pop-in');
		}
	});

}

// 画页面商家
function paintStoreUserWallet(usertype) {
    var html = [];
    html.push("<div class='grxinxis'>");
    
    
    html.push("<p><a onclick=\"clicked('collect/detail.html?t=all',false,false,'pop-in')\"><span><i class='angle right icon'></i> </span>可结算款（余额）</a></p>");
    html.push("<p><a onclick=\"clicked('collect/detail.html?t=cash',false,false,'pop-in')\"><span><i class='angle right icon'></i> </span>平台交易额</a></p>");
    html.push("<p><a onclick=\"clicked('collect/detail.html?t=pos',false,false,'pop-in')\"><span><i class='angle right icon'></i> </span>POS交易额</a></p>");
	html.push("<p><a><span><b id='frozen'>0.00</b><i class='angle right icon'></i> </span>商户保证金</a></p>");
    html.push("<p><a onclick=\"clicked('income.html?t=coin', false, false, 'pop-in')\"><span><b id='coin'>0</b> <i class='angle right icon'></i> </span>金币</a></p>");

    if (usertype == 2) {
        html.push("<p id='transfer'><a href='javascript:void(0);'><span> <b>&nbsp;</b> <i class='angle right icon'></i> </span>余额转出</a></p>");
        html.push("<p id='bbnone'><a href='javascript:void(0);'><span> <b>&nbsp;</b> <i class='angle right icon'></i> </span>银行卡  </a></p>");
    }
    html.push("</div>");

    $("#itemlist").html(html.join(""));
}

// 画其他会员
function paintAccontUserWallet() {
    var html = [];
    html.push("<div class='grxinxis'>");
    html.push("<p><a onclick=\"clicked('income.html?t=cash',false,false,'pop-in')\"><span><b id='cash'> 0.00元</b> <i class='angle right icon'></i> </span>余额</a></p>");
    html.push("<p id='membercard'><a onclick=\"clicked('income.html?t=card', false, false, 'pop-in')\"><span><b id='card'> 0.00元</b> <i class='angle right icon'></i> </span>会员卡</a></p>");
    html.push("<p><a onclick=\"clicked('income.html?t=coin', false, false, 'pop-in')\"><span><b id='coin'>0</b> <i class='angle right icon'></i> </span>金币</a></p>");
    html.push("<p id='charge'><a href='javascript:void(0);'><span> <b>&nbsp;</b> <i class='angle right icon'></i> </span>充值</a></p>");
    html.push("<p id='transfer'><a href='javascript:void(0);'><span> <b>&nbsp;</b> <i class='angle right icon'></i> </span>余额转出</a></p>");
    html.push("<p id='bbnone'><a href='javascript:void(0);'><span> <b>&nbsp;</b> <i class='angle right icon'></i> </span>银行卡  </a></p>");
    html.push("</div>");

    $("#itemlist").html(html.join(""));
}


// 画广告位
function PaintAdv() {
    // 头部
    $.ajax({
        type: 'Get',
        url: configManager.RequstUrl + "api/ads/posi?mark=moneybag"
    }).done(function (data) {
        if (data.state != "success") { console.log(data.message); return; }
        if (!data.data.ads || data.data.ads.length <= 0) { return; }

        var html = [];
        html.push("<div class='mui-slider-group mui-slider-loop'>");

        for (var i = 0; i < data.data.ads.length; i++) {
            var adv = data.data.ads[i];
            var src = configManager.adsImgurl.format(adv.pic, "") + '640-220';
            if (i == 0) {
                html.push("<div class='mui-slider-item mui-slider-item-duplicate'><a href='" + adv.url + "'><img src='" + src + "'></a></div>");
            }

            html.push("<div class='mui-slider-item'><a href='" + adv.url + "'><img src='" + src + "'></a></div>");

            if (i == data.data.ads.length - 1) {
                html.push("<div class='mui-slider-item mui-slider-item-duplicate'><a href='" + adv.url + "'><img src='" + src + "'></a></div></div>");
            }
        }

        html.push("<div class='mui-slider-indicator'>");
        for (var i = 0; i < data.data.ads.length; i++) {
            var indi = (i == 0) ? "<div class='mui-indicator mui-active'></div>" : "<div class='mui-indicator'></div>";
            html.push(indi);
        }
        html.push("</div>");
       
        $("#slider").html(html.join(""));
        settingAdvScroll();
    });
}

// 设置广告滚动
function settingAdvScroll() {
    mui.init({
        swipeBack: true //启用右滑关闭功能
    });
    var slider = mui("#slider");
    slider.slider({ interval: 3000 });
}

